using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class RopeItem : BaseItem
	{
		private string targetId;

		private string targetName;

		private Transform target;

		private HingeJoint2D joint;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			string[] array = base.itemData.scriptParams.Split(',');
			targetId = array[0];
			targetName = array[1];
		}

		protected override void LateUpdate()
		{
			if (target != null)
			{
				base.transform.SetZ(target.position.z + 1E-05f);
			}
		}

		public override void ResetState()
		{
			base.ResetState();
			InitJoint();
		}

		private void InitJoint()
		{
			BaseItem itemById = Singleton<Scene>.Instance.GetItemById(targetId);
			if (itemById != null)
			{
				target = itemById.model.Find(targetName);
				if (!(target != null))
				{
					return;
				}
				Transform transform = model.Find("0");
				Transform child = model.GetChild(model.childCount - 1);
				HingeJoint2D[] componentsInChildren = child.GetComponentsInChildren<HingeJoint2D>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					if (componentsInChildren[i].connectedBody == null)
					{
						joint = componentsInChildren[i];
						componentsInChildren[i].connectedBody = target.GetComponent<Rigidbody2D>();
						break;
					}
				}
				if (transform != null && typeof(HadRopeItem).IsAssignableFrom(itemById.GetType()))
				{
					((HadRopeItem)itemById).SetOriginalPoint(transform);
				}
				Line component = child.GetComponent<Line>();
				component.point2 = target;
			}
			else
			{
				Trace.LogWarning(base.itemData.id + "无法找到目标物件");
			}
		}

		public void ResetJoint()
		{
			if (joint != null)
			{
				joint.enabled = false;
				joint.enabled = true;
			}
		}
	}
}
